#include<stdio.h>
#include<stdlib.h>
#undef DEBUG
#define BFS
int nodes;
int pairs;

struct adjClique{
	int obj;
	struct adjClique *next;
};

typedef struct adjClique adjClique;


struct clique{
	int start;
	int end;
	int liars;
	int processed;
	adjClique *neighbours;
};

typedef struct clique clique;


adjClique *bfs;
clique *cliqueArr;
clique *curr;

int *nodesArr;
void update(int p1,int p2,clique *);
int checkIntersection(int,int,int,int);
void addNeighbour(int,int);
void updateBFS(int);
void process(int);
int findMaxLoc(int *);
void displayNodeArr();
void addClique(int index,int p1,int p2,int p3){


		cliqueArr[index].start=p1;
		cliqueArr[index].end=p2;
		cliqueArr[index].liars=p3;
		cliqueArr[index].neighbours=NULL;
		cliqueArr[index].processed=-1;

	
}

void createGraph(){
	int i,j;
	for(i=0;i<pairs;i++){
		for(j=i+1;j<pairs;j++){
			if(checkIntersection(cliqueArr[i].start,cliqueArr[i].end,cliqueArr[j].start,cliqueArr[j].end)){
				addNeighbour(i,j);
			}
		}
	}
}


int checkIntersection(int p11, int p12, int p21, int p22){
#ifdef DEBUG
	printf("%d,%d,%d,%d",p11,p12,p21,p22);
#endif
	if(p12>=p21&&p12<=p22)
		return 1;
	else 
		return 0;
}

void displayGraph(){
	int i=0;
	int count=0;
	for(i=0;i<pairs;i++){
#ifdef DEBUG
		printf("node %d starts %d end %d liars %d\n",i,cliqueArr[i].start,cliqueArr[i].end,cliqueArr[i].liars);
#endif
		count=0;
		adjClique *ptr=cliqueArr[i].neighbours;
		while(ptr!=NULL){
			printf("%d neighbour with %d\n",i,ptr->obj);
		
			ptr=ptr->next;
		}
	}
}

void addNeighbour(int i,int j){
#ifdef DEBUG
	printf("%d called %d\n",i,j);
#endif
	adjClique *ptr=cliqueArr[i].neighbours;
	adjClique *temp=(adjClique*)malloc(sizeof(adjClique));
	temp->obj=j;
	temp->next=NULL;

	if(ptr==NULL){
		cliqueArr[i].neighbours=temp;
	}
	else{
		while(ptr->next!=NULL){
			ptr=ptr->next;
		}
		ptr->next=temp;
	}
	ptr=cliqueArr[j].neighbours;
	adjClique *temp1=(adjClique*)malloc(sizeof(adjClique));
	temp1->obj=i;
	temp1->next=NULL;

	if(ptr==NULL){
		cliqueArr[j].neighbours=temp1;
	}
	else{
		while(ptr->next!=NULL){
			ptr=ptr->next;
		}
		ptr->next=temp1;
	}
}

void bfsStart(int i){
	bfs=(adjClique*)malloc(sizeof(adjClique));
	bfs->obj=i;
	bfs->next=NULL;
	adjClique *ptr=bfs;
	while(ptr!=NULL){
		if(cliqueArr[ptr->obj].processed==1){
			ptr=ptr->next;
			continue;
		}
		process(ptr->obj);
		printf("\n After processing %d clique\n",ptr->obj);
		displayNodeArr();
		cliqueArr[ptr->obj].processed=1;
		updateBFS(ptr->obj);
		ptr=ptr->next;
		getchar();
	}
	
}

void updateBFS(int index){
	adjClique *ptr=bfs;
	while(ptr->next!=NULL){
		ptr=ptr->next;
	}	
	adjClique *ptr1=cliqueArr[index].neighbours;
	while(ptr1!=NULL){
		if(cliqueArr[ptr1->obj].processed==-1){
			adjClique *temp=(adjClique*)malloc(sizeof(adjClique));
			temp->obj=ptr1->obj;
			temp->next=NULL;
			ptr->next=temp;
			ptr=ptr->next;
		}

			ptr1=ptr1->next;
	}
}
void process(int a){
	int liar=	cliqueArr[a].liars;
	int i;
	for(i=cliqueArr[a].start;i<=cliqueArr[a].end;i++){
		if(nodesArr[i-1]==1){
			liar--;
		}
	}
	int tempArr[nodes];
	for(i=0;i<nodes;i++){
		tempArr[i]=0;
	}
	
	adjClique *ptr=cliqueArr[a].neighbours;
	while(ptr!=NULL){
		int j=0;
		for(j=cliqueArr[ptr->obj].start-1;j<=cliqueArr[ptr->obj].end-1;j++){
			tempArr[j]++;	
		}
		ptr=ptr->next;
	}
	#ifdef BFS
		printf("%d new liars to be allocated in %d clique\n",liar,a);
	#endif
	while(liar>0){
		int loc=findMaxLoc(tempArr);
		if(loc>=cliqueArr[a].start-1 && loc<=cliqueArr[a].end-1 && nodesArr[loc]!=1){
			
			nodesArr[loc]=1;
			liar--;
			
		}
		
	}
	
}
int findMaxLoc(int *tempArr){
	int j=0;
	int max=1;
	for(j=2;j<nodes+1;j++){
		if(tempArr[max]<tempArr[j]){
			max=j;
		}
	}
	tempArr[max]=0;
	return max;
}
void displayNodeArr(){
	int i=0;
	for(i=0;i<nodes;i++)	
	{	
		printf("%d->%d\n",(i+1),nodesArr[i]);
	}
	printf("done\n\n");
}

int main(){

	int i;
	int p1,p2,p3;
	scanf("%d %d",&nodes,&pairs);

	cliqueArr=(clique*)malloc(sizeof(clique)*pairs);
	nodesArr=(int*)malloc(sizeof(int)*nodes);

	for(i=0;i<nodes;i++){
		nodesArr[i]=0;
	}

	for(i=0;i<pairs;i++){
		scanf("%d %d %d",&p1,&p2,&p3);
		addClique(i,p1,p2,p3);
	}

	createGraph();
	displayGraph();
	bfsStart(0);

	for(i=0;i<nodes;i++){
		printf("%d->%d\n",(i+1),nodesArr[i]);
	}
	
}
